package 数据库连接;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @作者:侯国威
 * @时间: 2020/4/11/12:29
 * @邮箱:1045077189@qq.com
 */
public class jdbc_test {
//    private static final String DIRVER_NAME = "com.mysql.cj.jdbc.Driver";
//    //提供链接的数据库
//    private static final String URL = "jdbc:mysql://localhost:3306/stu";
//    private static final String USER_NAME = "root";
//    //密码
//    private static final String PASSWORD = "1641112";
//方式一
//    public static void main(String[] args) throws SQLException {
//        //获取Driver实现类的对象，使用反射
//        Driver driver =new com.mysql.cj.jdbc.Driver();
//        Properties info = new Properties();
//        String url="jdbc:mysql://localhost:3306/stu";
//
//        info.setProperty("user","root");
//        info.setProperty("password","1641112");
//        Connection connection =driver.connect(url,info);
//        System.out.println(connection);
//    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
            jdbc_test t1 =new jdbc_test();
            t1.SQLTEST2();
    }


//    public void SQLTEST() throws SQLException{
//        Connection connection = null;
//        try {
//            //加载驱动
//
//            Class.forName(DIRVER_NAME);
//            connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
//            //写sql语句
//            String sql = "SELECT id,password,user FROM student";
//            //执行语句
//            PreparedStatement preparedStatement = connection.prepareStatement(sql);
//            //获取结果
//            ResultSet resultSet = preparedStatement.executeQuery();
//            System.out.println("id" + "\t" + "user" + "\t" + "password");
//            while (resultSet.next()) {
//
//                System.out.println(resultSet.getString("id") + "\t" + resultSet.getString("user") + "\t" + resultSet.getString("password"));
//
//            }
//            resultSet.close();
//            preparedStatement.close();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } finally {
//            if (connection != null) {
//                try {
//                    ((Connection) connection).close();
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }


    public  void SQLTEST2() throws IOException, ClassNotFoundException, SQLException {
        //方式二:数据库需要加载的信息放到配置文件中
        //1、读取配置文件的信息
        InputStream is=jdbc_test.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties=new Properties();
        assert is != null;
        properties.load(is);
        String user=properties.getProperty("user");
        String password=properties.getProperty("password");
        String url=properties.getProperty("url");
        String dirver=properties.getProperty("dirver");
        //加载驱动
        Class.forName(dirver);
        //获取连接
        Connection connection=DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

}
